// src/components/PermissionButton.jsx
import React, { FC } from 'react';
import { getPagePath } from '@/utils';

interface IProps {
  permission: string;
  children: any;
}

const PermissionButton: FC<IProps> = ({ permission, children }) => {
  if (localStorage.getItem('permission')) {
    return children;
  }
  // 1. 获取页面下的权限 (通过路径匹配获取当前路由信息，然后获取路由信息下的权限)
  const permissions = getPagePath().action?.split(',') || [];

  // 2. 权限校验
  return permissions.includes(permission) ? children : null;
  // return children;
};

interface IParams {
  permission?: any;
  code?: any;
}

export const isPermission = (params: IParams) => {
  if (localStorage.getItem('permission')) {
    return true;
  }
  const { permission, code } = params;
  const permissions = getPagePath().action?.split(',') || [];
  // if (Object.prototype.toString.call(permissions) === '[object Object]') {
  //   if (permission) {
  //     return permissions[code]?.includes?.(permission);
  //   }
  //   return permissions[code];
  // }
  return permissions?.includes?.(permission);
};

export const isPermissions = (params: IParams) => {
  if (localStorage.getItem('permission')) {
    return true;
  }
  const { permission, code } = params;
  const permissions = getPagePath().action?.split(',') || [];

  return permissions?.some(item => permission?.includes(item) );
};

export const isPermissionOperate = () => {
  return getPagePath().action;
};

export default PermissionButton;
